package com.kgc.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.kgc.api.buylimit.CouponApi;
import com.kgc.bean.*;
import com.kgc.mapper.DiscountTrickMapper;
import com.kgc.mapper.UserDiscountTrickMapper;

import com.kgc.mapper.UserMapper1;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class DiscountProvider implements CouponApi {
    @Autowired
    private DiscountTrickMapper discountTrickMapper;
    @Autowired
    private UserMapper1 userMapper1;
    @Autowired
    private UserDiscountTrickMapper userDiscountTrickMapper;
    @Override
    public boolean addDiscountTrick(DiscountTrick discountTrick) {
        if (discountTrickMapper.adddiscount(discountTrick)>0){
            return true;
        }
        return false;
    }

    @Override
    public ReturnResult<String> userAddDiscountTrick(Integer userId, Integer discountTrickId) {
        DiscountTrick discountTrick = discountTrickMapper.selectByPrimaryKey(discountTrickId);
        if (discountTrick.getNum() > 0) {
            User user = userMapper1.selectByPrimaryKey(userId);
            if (user.getIntegral() >= discountTrick.getRobIntegral()) {
                user.setIntegral(user.getIntegral() - discountTrick.getRobIntegral());
                int i = userMapper1.updateByPrimaryKeySelective(user);
                discountTrick.setNum(discountTrick.getNum()-1);
                discountTrickMapper.updateByPrimaryKeySelective(discountTrick);
                if (i > 0) {
                    UserDiscountTrick userDiscountTrick = new UserDiscountTrick();
                    userDiscountTrick.setDiscountId(discountTrick.getId());
                    userDiscountTrick.setDistickBeginTime(discountTrick.getStartTime());
                    userDiscountTrick.setDistickEndTime(discountTrick.getEndTime());
                    userDiscountTrick.setUserId(user.getUserId());
                    int i1 = userDiscountTrickMapper.insertSelective(userDiscountTrick);
                    if (i1 > 0) {
                        return ResultBuildUtil.success("优惠券兑换成功");
                    }
                } else {
                    return ResultBuildUtil.fail("1005", "兑换失败");
                }
            } else {
                return ResultBuildUtil.fail("1004", "兑换失败，积分不足");
            }
        }
        return ResultBuildUtil.fail("1002", "库存不足");
    }


    @Override
    public boolean checkonly(Integer userId, Integer discountTrickId) {
        UserDiscountTrickExample userDiscountTrickExample = new UserDiscountTrickExample();
        UserDiscountTrickExample.Criteria criteria = userDiscountTrickExample.createCriteria();
        criteria.andDiscountIdEqualTo(discountTrickId);
        criteria.andUserIdEqualTo(userId);
        List<UserDiscountTrick> userDiscountTricks = userDiscountTrickMapper.selectByExample(userDiscountTrickExample);
        return null == userDiscountTricks || userDiscountTricks.size() == 0;
    }

    @Override
    public List<DiscountTrick> getEnableDiscountTrick() {
        DiscountTrickExample discountTrickExample = new DiscountTrickExample();
        DiscountTrickExample.Criteria criteria = discountTrickExample.createCriteria();
        criteria.andEndTimeGreaterThan(new Date()).andNumGreaterThan(0);
        return discountTrickMapper.selectByExample(discountTrickExample);
    }

    @Override
    public User selectInfobyId(Integer userId) {

        return userMapper1.selectByPrimaryKey(userId);
    }

    @Override
    public DiscountTrick querySelectDiscountTrickById(Integer id) {
        return discountTrickMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean justyUserUsedDiscount(Integer userId, Integer discountId) {

        UserDiscountTrickExample userDiscountTrickExample = new UserDiscountTrickExample();
        UserDiscountTrickExample.Criteria criteria = userDiscountTrickExample.createCriteria();
        criteria.andDiscountIdEqualTo(discountId);
        criteria.andUserIdEqualTo(userId);
        criteria.andDiscountStatusEqualTo(0);
        criteria.andDistickBeginTimeLessThan(new Date());
        criteria.andDistickEndTimeGreaterThan(new Date());
        List<UserDiscountTrick> userDiscountTricks = userDiscountTrickMapper.selectByExample(userDiscountTrickExample);
        return null == userDiscountTricks || userDiscountTricks.size() == 0;
    }


}
